package com.boot.myaop;

import com.boot.myannotation.MyLog;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Optional;

/**
 * @author MShaoJie
 * @create 2021/8/25 14:19
 **/
@Component
@Slf4j
@Aspect
public class LogAspect {

    @Pointcut("@annotation(com.boot.myannotation.MyLog)")
    public void aspect() {
//        log.info("aspect 方法执行 +++++");
    }

    @Before("aspect()")
    public void before() {
//        log.info("before 方法执行 +++++");
    }

    @After("aspect()")
    public void after(JoinPoint joinPoint) {
//        log.info("after 方法执行 +++++");
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //切入方法
        Method method = signature.getMethod();
        //获取参数值
        Object[] args = joinPoint.getArgs();
        //参数名
        String[] parameterNames = signature.getParameterNames();
        //组合传入参数：参数名-参数值
        HashMap<String, Object> paramsHashMap = new HashMap<>();
        for (int i = 0; i < parameterNames.length; i++) {
            paramsHashMap.put(parameterNames[i], args[i]);
        }
        //方法名称
        String methodName = method.getName();
        //获取注解的内容
        MyLog annotation = method.getAnnotation(MyLog.class);
        String explain = annotation.operateExplain();
        //当前服务器时间
        String nowTime = getNowTimeStr();
        log.info("方法：{}-{},完成时间：{}，传入参数：{} ", methodName, explain, nowTime, paramsHashMap.toString());
    }

    @AfterReturning(pointcut = "aspect()", returning = "rvt")
    public void afterReturing(String rvt) {
//        log.info("afterReturing 最终方法执行 +++++");
    }

    @AfterThrowing(pointcut = "aspect()", throwing = "e")
    public void afterThrowing(Throwable e) {
//        log.info("afterThrowing 方法执行" + e);
    }

//    @Around("aspect()")
//    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
//        log.info("around 方法执行");
//        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        Method method = signature.getMethod();
//        MyLog annotation = method.getAnnotation(MyLog.class);
//        log.info("around  operateType： " + annotation.operateType());
//        log.info("around  operateExplain： " + annotation.operateExplain());
//
//
//        Object[] args = joinPoint.getArgs();
//        String[] argNames = signature.getParameterNames();
//        HashMap<String, Object> argNamesHashMap = new HashMap<>();
//        for (int i = 0; i < argNames.length; i++) {
//            argNamesHashMap.put(argNames[i], args[i]);
//        }
//        log.info("入参" + argNamesHashMap.toString());
//        Object proceed = joinPoint.proceed();
//        log.info("出参" + Optional.ofNullable(proceed));
//        return proceed;
//    }
    /******
     * @Description 获取当前时间 yyyy-MM-dd HH:mm:ss 格式
     * @return
     */
    private String getNowTimeStr() {
        long nowTime = System.currentTimeMillis();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(nowTime);
        return simpleDateFormat.format(date);
    }
}
